package server.main.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import server.main.mapper.RoleMapper;
import server.main.mapper.RolePermissionMapper;
import server.main.pojo.Permission;
import server.main.pojo.Role;
import server.main.pojo.RolePermission;
import server.main.service.RolePermissionService;

import java.util.*;

/**
 * @auther MingHsZzz
 * @Date 2022/10/5  10:24
 * @project main
 * @description
 */
@Service("RolePermissionService")
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements RolePermissionService {


   @Autowired
   RolePermissionMapper rolePermissionMapper;

   @Autowired
   RoleMapper roleMapper;

   @Override
   public void saveRolePermissionBatch(Role role) {
      List<RolePermission> savedata = new ArrayList<>();
      if(role.getPermissions() != null){
         //将permission_id与roleid相连,批量注入
         for (Permission permission : role.getPermissions()) {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(role.getId());
            rolePermission.setPrtmissionId(permission.getId());
            savedata.add(rolePermission);
         }
         saveBatch(savedata);
      }


   }

   @Override
   public boolean updatePermissionBatch(Role prerole, Role newrole) {
      List<RolePermission> savedata = new ArrayList<>();
      List<Integer> deletedata = new ArrayList<>();
      List<Integer> newdata = new ArrayList<>();
      List<Integer> predata = new ArrayList<>();

      //都删除了可能也会出问题，在都删除时会返回空的permissions
      if(newrole.getPermissions() == null && prerole.getPermissions() == null){
         System.out.println("都为空不进行任何操作");
      }else if(newrole.getPermissions() == null && prerole.getPermissions() != null){
         System.out.println("newrole为空");
         for (Permission permission : prerole.getPermissions()) {
            deletedata.add(permission.getId());
         }
      }else if(newrole.getPermissions() != null && prerole.getPermissions() == null){
         System.out.println("prerole为空");
         for (Permission permission : newrole.getPermissions()) {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(newrole.getId());
            rolePermission.setPrtmissionId(permission.getId());
            savedata.add(rolePermission);
         }

      }else{
         for (Permission permission : newrole.getPermissions()) {
            newdata.add(permission.getId());
         }
         for (Permission permission : prerole.getPermissions()) {
            predata.add(permission.getId());
         }
         for (Integer newdatum : newdata) {
            if(!predata.contains(newdatum)){
               RolePermission rolePermission = new RolePermission();
               rolePermission.setRoleId(newrole.getId());
               rolePermission.setPrtmissionId(newdatum);
               savedata.add(rolePermission);
            }
         }
         //找到删除的
         for (Integer predatum : predata) {
            if(!newdata.contains(predatum)){
               deletedata.add(predatum);
            }
         }
      }
//      saveBatch(savedata);
      System.out.println("保存的数据：" + savedata);
      System.out.println("删除的数据" + deletedata);
      if(deletedata.size() > 0){
         for (Integer deletedatum : deletedata) {
            System.out.println("删除的" + newrole.getId() + deletedatum);
            rolePermissionMapper.delete(newrole.getId(),deletedatum);
         }
      }

      if(savedata.size() > 0){
         for (RolePermission savedatum : savedata) {
            save(savedatum);
         }
      }


      return true;
   }



}
